<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        //一.接收form表单数据
        echo "loginAction<br>";
        var_dump($_POST);
        echo "<br>";
        $uname=$_POST['uname'];
        $upwd=$_POST['upwd'];
        echo "$uname $upwd"; 
        //二.处理表单数据，操作数据表
        //1.连接数据库
        $link=mysqli_connect("localhost","root","");//创建数据库，返回数据库连接
        mysqli_select_db($link, "nhcar");//选择要使用的数据库
        mysqli_set_charset($link, "utf8");//设置连接的字符编码
        //2.操作数据表
        $sql="SELECT * FROM users WHERE uname='$uname' AND upwd='$upwd';";
        $result=mysqli_query($link,$sql);
        //echo "<br>$sql</br>";
        //$row=mysqli_fetch_row($result);//返回一行数据索引数组
        $row= mysqli_fetch_array($result);//返回一行数据包括索引数组和关联数组
        //$row=mysqli_fetch_assoc($result);//返回一行关联数组
        var_dump($row);
        //exit();
        //3.处理结果数据,并返回
        if($row){
            //cookie储存用户ID、用户名
            $uid=$row['uid'];
            $uname=$row['uname'];
             //setcookie(键、值、过期时间);//过期时间=当前时间+时间间隔
            setcookie("nhuid",$uid,time()+20);//3600秒*24小时*7天
            setcookie("nhuname",$uname,time()+20);
            echo "<script>alert('恭喜你！登录成功！');location.href='index.php'</script>";
        }else {
            echo "<script>alert('抱歉！用户或密码错误！');location.href='login.php'</script>";
        }
        ?>
    </body>
</html>
